package com.yeqian.controller;

import com.yeqian.entity.ImageUpload;
import com.yeqian.entity.Result;
import com.yeqian.utils.DateUtils;
import com.yeqian.utils.OSSUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @Author : yeqian
 * @Date : 2020/12/26 15:15
 * 图片控制器
 */
@RestController
@RequestMapping("/image")
public class ImageController extends BaseController {

   // private Logger logger = LogUtils.getInstance(ImageController.class);

    /**
     * 管理员头像
     *
     * @param file
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/3 17:06
     */
    @PostMapping("/userAvatarUpload")
    public Result userAvatar(@RequestParam("imgFile") MultipartFile file) {
        String filePath = "userAvatar/" + DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        String img = OSSUtils.upload(file, filePath);
        return new Result(true, "管理员头像成功", img);
    }

    /**
     * 标签封面上传
     *
     * @param file
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/1 12:26
     */
    @PostMapping("/tagUpload")
    public Result tagUpload(@RequestParam("imgFile") MultipartFile file) {
        String filePath = "Cover/tag/" + DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        String img = OSSUtils.upload(file, filePath);
        return new Result(true, "标签封面上传成功", img);
    }


    /**
     * 博客封面上传
     *
     * @param file
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2021/1/1 12:26
     */
    @PostMapping("/blogUpload")
    public Result BlogUpload(@RequestParam("imgFile") MultipartFile file) {
        String filePath = "Cover/blog/" + DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        String img = OSSUtils.upload(file, filePath);
        return new Result(true, "博客封面上传成功", img);
    }


    /**
     * 博客图片上传
     *
     * @param file
     * @return com.yeqian.entity.Result
     * @author yeqian
     * @date 2020/12/26 15:32
     */
    @PostMapping("/upload")
    public ImageUpload upload(@RequestParam("editormd-image-file") MultipartFile file) {
        String filePath = "article/"+DateUtils.parseDate2String(new Date(), "yyyy-MM-dd");
        String upload = OSSUtils.upload(file, filePath);
        return new ImageUpload("博客图片上传成功", 1, upload);
    }



}
